var api = require('../../utils/api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _api: {},
    company: {}, //公司资料
    delivery: [], //交车图片
    recommended: {}, //主营
    car_arr: {}, //车列表
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 100,
    hiddenmodal: true, //隐藏模态框
    hiddenBindMobile:true, //弹框提示绑定手机
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    //检查是否是分享的页面
    if (typeof options.uid != undefined && options.uid > 0) {
      //保存分享的uid
      try {
        wx.setStorageSync('share_uid', options.uid)
      } catch (e) { }
    }

    try {
      var isLogin = wx.getStorageSync('login');
      if (!isLogin) {
        api.login();
        return;
      }
    } catch (e) {
      // Do something when catch error
    }

    this.setData({
      _api: api,
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    //公司资料
    try {
      var company = wx.getStorageSync('company')
      if (company) {
        that.setData({
          company: company
        });
      } else {
        that.getIndexInfo();
      }
    } catch (e) {
      that.getIndexInfo();
    }

    //主营车型
    try {
      var recommended = wx.getStorageSync('recommended')
      if (recommended) {
        that.setData({
          recommended: recommended
        });
      } else {
        that.getIndexInfo();
      }
    } catch (e) {
      that.getIndexInfo();
    }

    //车型报价
    try {
      var car_arr = wx.getStorageSync('car_arr_index')
      if (car_arr) {
        that.setData({
          car_arr: car_arr
        });
      } else {
        that.getIndexInfo();
      }
    } catch (e) {
      that.getIndexInfo();
    }

    //交车图片
    try {
      var delivery = wx.getStorageSync('delivery_index')
      if (delivery) {
        that.setData({
          delivery: delivery
        });
      } else {
        that.getIndexInfo();
      }
    } catch (e) {
      that.getIndexInfo();
    }

    //判断是否弹出绑定手机号框
      var user = wx.getStorageSync('user')
      if (user) {
        //判断是否有moblie
        if(user.mobile == ''){
          //如果今天还没弹框
          var today = (new Date()).format("yyyy-M-d");
          var show_bind_mobile_date = wx.getStorageSync('show_bind_mobile_date')
          if ( show_bind_mobile_date != today ){
              that.setData({
                hiddenBindMobile:false
              });

              //无论按了取消还是确定，缓存更新今天已经点击不在提示
              wx.setStorage({
                key: "show_bind_mobile_date",
                data: today
              })

            wx.showModal({
              title: '提示',
              content: '请前往绑定手机',
              success: function (res) {
                if (res.confirm) {
                  //跳转
                  wx.navigateTo({
                    url: '/pages/bindingTel/bindingTel',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    this.getIndexInfo();

    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    //获取用户ID
    var uid = 0;

    try {
      var user = wx.getStorageSync('user')
      if (user) {
        var uid = user.id
      }
    } catch (e) {
      // Do something when catch error
    }

    return {
      title: '乾丰车行',
      path: '/pages/index/index?uid=' + uid
    }
  },
  /**
   * 预览图片
   */
  previewImage: function (e) {
    console.log(e)
    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.delivery // 需要预览的图片http链接列表  
    })
  },
  /**
   * 获取首页信息
   */
  getIndexInfo: function () {
    var that = this;
    api.get({
      url: 'portal/index',
      success: data => {
        console.log(data)
        if (data.code) {
          //公司资料
          var company = data.data.company;

          //交车图片
          var delivery = data.data.delivery;

          //主营车型
          var recommended = data.data.recommended;

          //车型列表
          var car_arr = data.data.car_arr;

          //加入缓存
          wx.setStorage({
            key: "company",
            data: company
          })
          wx.setStorage({
            key: "recommended",
            data: recommended
          })
          wx.setStorage({
            key: "car_arr_index",
            data: car_arr
          })
          wx.setStorage({
            key: "delivery_index",
            data: delivery
          })

          //保存数据到data
          that.setData({
            company: company,
            delivery: delivery,
            recommended: recommended,
            car_arr: car_arr
          });

        } else {
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail: err => { }
    });
  },
  /**
   * 点击显示地图
   */
  showMap: function () {
    var that = this;
    //获取经纬度
    var lng = Number(that.data.company.lng);
    var lat = Number(that.data.company.lat);

    wx.openLocation({
      latitude: lat,
      longitude: lng,
      scale: 28
    })
  },
  /**
   * 主营车型跳转
   */
  click_set: function (e) {
    console.log(e)
    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    var id = e.currentTarget.id;
    wx.setStorageSync('ver_id', id)
    wx.setStorageSync('sort_type', 0)
    //车型报价页面 左侧选中
    var top_left = e.currentTarget.dataset.value;
    wx.setStorageSync('top_left', top_left)

    wx.switchTab({
      url: '/pages/modelsOffer/modelsOffer',
    })
  },
  /**
   * 点击车型报价更多
   */
  more_car: function (e) {

    console.log(e)
    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    wx.setStorageSync('ver_id', 0)
    wx.setStorageSync('sort_type', 0)
    wx.setStorageSync('top_left', '全部品牌')

    wx.switchTab({
      url: '/pages/modelsOffer/modelsOffer',
    })
  },
  formSubmit: function (e) {
    console.log(e)
    var form_id = e.detail.formId;
    // var url = 
  },
  /**
   * 交车 更多
   */
  more_delivery: function (e) {

    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    //跳转
    wx.navigateTo({
      url: '/pages/carPhotos/carPhotos',
    })
  },
  /**
   * 导航到汽车详情
   */
  nav_detail: function (e) {
    console.log(e)

    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    var url = e.target.dataset.url;
    console.log(url)
    wx.navigateTo({
      url: url,
    })
  },
  clickTel: function (e) {
    console.log(e)

    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    this.setData({
      hiddenmodal: false
    })
  }, //打电话显示弹层
  clickCancel: function (e) {
    this.setData({
      hiddenmodal: true
    })
  },   //取消打电话弹层
  //打电话
  callAction: function (e) {
    var callTel = e.currentTarget.dataset.value;
    wx.makePhoneCall({
      phoneNumber: callTel
    })
  }
})